package com.example.mobile2030606148tdh0514w;

import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainContent;
    private String url="http://192.168.29.230:3000/news";
private OkHttpClient okHttpClient=new OkHttpClient();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Request request=new Request.Builder()
                        .url(url)
                        .build();
                Call call=okHttpClient.newCall(request);
                try {
                   Response response=call.execute();
                   String result=response.body().string();
                   runOnUiThread(new Runnable() {
                       @Override
                       public void run() {
                           tvMainContent.setText(result);
                       }
                   });



                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initView() {
        tvMainContent = (TextView) findViewById(R.id.tvMainContent);
    }
}